package cn.edu.hgu.controller;

import cn.edu.hgu.dao.impl.NewsDaoImpl;
import cn.edu.hgu.entity.News;
import cn.edu.hgu.service.NewsService;
import cn.edu.hgu.service.impl.NewsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class NewsServlet extends HttpServlet {


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           response.setContentType("text/html;charset=utf-8");
           request.setCharacterEncoding("utf-8");
           String method=request.getParameter("method"); //获取方法名
           if(method.equals("query")){
               String title=request.getParameter("title");
               String content=request.getParameter("content");
               String type=request.getParameter("type");
               String publisher=request.getParameter("publisher");
               String department=request.getParameter("department");
               News news=new News();
               news.setTitle(title);
               news.setContent(content);
               news.setType(type);
               news.setPublisher(publisher);
               news.setDepartment(department);
               NewsService newsService=new NewsServiceImpl();
               List<News> newsList=newsService.getListAll(news);
               request.getSession().setAttribute("newsList",newsList);
               request.getRequestDispatcher("/WEB-INF/view/query.jsp").forward(request,response);
           }

           if(method.equals("addNew")){//进入新增页面
               request.getSession().removeAttribute("news");
               request.getRequestDispatcher("/WEB-INF/view/new.jsp").forward(request,response);
           }

           if(method.equals("add")){//提交新增数据
               String title=request.getParameter("title");
               String content=request.getParameter("content");
               String type=request.getParameter("type");
               String publisher=request.getParameter("publisher");
               String department=request.getParameter("department");
               String count=request.getParameter("count");
               String id=request.getParameter("id");
               List<Object> objects=new ArrayList<>();
               objects.add(title);
               objects.add(content);
               objects.add(type);
               objects.add(publisher);
               objects.add(department);
               objects.add(count);

               NewsService newsService=new NewsServiceImpl();
               if(id!=null&&!id.equals("")){
                   objects.add(id);
                   newsService.update(objects);
               }else{
                   newsService.add(objects);
               }
               response.sendRedirect("/NewsServlet?method=query");
           }

        if(method.equals("del")){//提交删除数据
            String id=request.getParameter("id");
            List<Object> objects=new ArrayList<>();
            objects.add(id);
            NewsService newsService=new NewsServiceImpl();
            newsService.del(objects);
            response.sendRedirect("/NewsServlet?method=query");
        }

        if(method.equals("editNew")){//进入编辑页面
            String id=request.getParameter("id");
            NewsService newsService=new NewsServiceImpl();
            News news=newsService.getId(id);
            request.getSession().setAttribute("news",news);
            request.getRequestDispatcher("/WEB-INF/view/new.jsp").forward(request,response);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         doPost(request,response);
    }
}
